package cn.yeamin.spring.boot.service;

import cn.yeamin.spring.boot.dao.GirlDao;
import cn.yeamin.spring.boot.pojo.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
@Service
@CacheConfig(cacheNames = "girls")
public class GirlService {

    @Autowired
    private GirlDao girlDao;

    //为了可以更好的观察，缓存的存储，注入cacheManager。通过Debug查看缓存管理器是EhCacheCacheManage@5724
    @Autowired
    private CacheManager cacheManager;


    @Cacheable(cacheNames = "girls" )
    public List<Girl> findAll() {
        return girlDao.selectAll();
    }

    @Cacheable(cacheNames = "girls")
    public Girl getOne(Integer id) {
        return girlDao.selectByPrimaryKey(id);
    }

    @CachePut(value = "girls",key = "#girl.id")
    public void save(Girl girl) {
        girlDao.insert(girl);
    }

    @CachePut(value = "girls",key = "#girl.id")
    public void update(Girl girl){
        //girlDao.updateByPrimaryKey(girl);
        girlDao.updateByPrimaryKeySelective(girl);
    }

    @CacheEvict(cacheNames = "girls", allEntries = true)
    public void deleteById(Integer id) {
        Girl girl = new Girl();
        girl.setId(id);
        girlDao.deleteByPrimaryKey(girl);
    }
}
